package com.mxpio.erp.plan.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;

import com.mxpio.erp.common.purchase.service.BuyRequestOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.plan.entity.PurchasePlan;
import com.mxpio.erp.common.plan.service.PurchasePlanService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "PurchasePlanController", description = "采购计划接口")
@RestController
@RequestMapping("/erp/plan/pp/")
public class PurchasePlanController {

	@Autowired
	private PurchasePlanService purchasePlanService;

	@Autowired
	private BuyRequestOrderService buyRequestOrderService;
	@GetMapping("list")
	@Operation(summary = "采购计划列表", description = "获取采购计划列表（无分页）", method = "GET")
	public Result<List<PurchasePlan>> list(@Parameter(description = "查询参数") Criteria criteria)
			throws UnsupportedEncodingException {
		List<PurchasePlan> purchasePlan = purchasePlanService.list(PurchasePlan.class, criteria);
		return Result.OK(purchasePlan);
	}

	@GetMapping("page")
	@Operation(summary = "采购计划列表", description = "获取采购计划列表（分页）", method = "GET")
	public Result<Page<PurchasePlan>> page(@Parameter(description = "查询参数") Criteria criteria,
			@Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo)
			throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		Page<PurchasePlan> purchasePlan = purchasePlanService.listPage(PurchasePlan.class, page, criteria);
		return Result.OK(purchasePlan);
	}

	@GetMapping("list/{planNo}")
	@Operation(summary = "根据planNo获取采购计划", description = "根据planNo获取采购计划", method = "GET")
	public Result<PurchasePlan> getById(
			@Parameter(description = "计划单号") @PathVariable(name = "planNo", required = true) String planNo) {
		PurchasePlan purchasePlan = purchasePlanService.getById(PurchasePlan.class, planNo);
		return Result.OK(purchasePlan);
	}

	@RequestMapping("save")
	@Operation(summary = "保存采购计划", description = "保存采购计划")
	public Result<PurchasePlan> save(@RequestBody PurchasePlan purchasePlan) {
		purchasePlanService.save(purchasePlan);
		return Result.OK(purchasePlan);
	}

	@DeleteMapping("remove/{planNos}")
	@Operation(summary = "根据planNo删除采购计划", description = "删除采购计划", method = "DELETE")
	@Transactional(readOnly = false)
	public Result<PurchasePlan> remove(
			@Parameter(description = "计划单号，多个单号用英文逗号分隔") @PathVariable(name = "planNos", required = true) String planNos) {
		String[] planNo = planNos.split(",");
		for (String key : planNo) {
			purchasePlanService.delete(PurchasePlan.class, key);
		}
		return Result.OK();
	}

	@RequestMapping("convert")
	@Operation(summary = "采购计划转请购单", description = "采购计划转请购单")
	public Result<Object> convert(@RequestBody List<PurchasePlan> planList) {
		return buyRequestOrderService.convertBROFromPPlan(planList);
	}

	@GetMapping("split/{planNo}/{splitNum}")
	@Operation(summary = "拆分制造计划", description = "拆分制造计划", method = "GET")
	public Result<PurchasePlan> spit(
			@Parameter(description = "计划单号") @PathVariable(name = "planNo", required = true) String planNo
			,@Parameter(description = "拆分数量") @PathVariable(name = "splitNum", required = true) BigDecimal splitNum) {
		return purchasePlanService.split(planNo, splitNum);
	}
}
